package www.baidu;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class textScoket implements Runnable {
	private static ServerSocket serverSocket;
	private Socket client;

	public textScoket(Socket sock) {
		this.client = sock;
	}

	public void run() {
		BufferedReader inBufferedReader = null;
		PrintWriter outPrintWriter = null;

		try {
			inBufferedReader = new BufferedReader(new InputStreamReader(
					client.getInputStream()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			outPrintWriter = new PrintWriter(client.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String text;

		try {
			while ((text = inBufferedReader.readLine()) != null) {
				if (text.equals("bye")) {
					break;
				}
				outPrintWriter.println(text);
				outPrintWriter.flush();
				System.out.println(text);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			inBufferedReader.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		outPrintWriter.close();

	}

	public static void main(String[] args) throws IOException {
		serverSocket = new ServerSocket(2999);
		System.out.println("start to listen at part 2999...");
		Socket clientSocket = serverSocket.accept();
		String cipString = clientSocket.getInetAddress().getHostAddress();
		System.out.println(cipString);

		while (true) {
			Socket client = serverSocket.accept();
			Thread thread = new Thread(new textScoket(client));
		}

	}

}
